package com.yuyou.fn.tradingservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.tradingservice.entity.LeaseMessage;
import com.yuyou.fn.tradingservice.entity.PersonLeaseMessage;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.mapper.PersonLeaseMessageMapper;
import com.yuyou.fn.tradingservice.service.ILeaseMessageService;
import com.yuyou.fn.tradingservice.service.IPersonLeaseMessageService;
import com.yuyou.fn.tradingservice.service.IServiceProductService;
import com.yuyou.fn.tradingservice.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * PersonLeaseMessage 表数据服务层接口实现类
 */
@Service
public class PersonLeaseMessageServiceImpl extends SuperServiceImpl<PersonLeaseMessageMapper, PersonLeaseMessage, Long> implements IPersonLeaseMessageService {

    @Resource
    private ILeaseMessageService leaseMessageService;

    @Resource
    private IServiceProductService serviceProductService;

    @Resource
    private ICampusService campusService;

    @Resource
    private ISummerWinterDateService summerWinterDateService;

    @Resource
    private IClassLectureService classLectureService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IRoomService roomService;

    private static String[] unitArray = {"小时", "天", "周", "月", "年"};

    private static String[] weekNameArray = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    private static Integer[] timeTypeArray = {3, 2, 1, 0, -1};

    private static Integer[] weekNumArray = {-1, 0, 1, 2, 3, 4, 5, 6};


    public Integer getTimeInterval(Integer timeType) {
        Integer timeInterval = 1;
        if (timeType == 0) {
            timeInterval = 1;
        } else if (timeType == 1) {
            timeInterval = 7;
        } else if (timeType == 2) {
            timeInterval = 30;
        } else if (timeType == 3) {
            timeInterval = 365;
        }
        return timeInterval;
    }

    @Override
    public List<PersonLeaseMessage> findPersonLeaseMessage(PersonLeaseQueryVo personLeaseQueryVo) {

        Example<PersonLeaseMessage> example = PersonLeaseMessage.createExample();

        Example.Criteria criteria = example.createCriteria();

        if (personLeaseQueryVo.getDateQueryCode() != null && personLeaseQueryVo.getDateQueryCode() == 1 && personLeaseQueryVo.getStart() != null && personLeaseQueryVo.getEnd() != null) {
            example.or().andBetween("end", personLeaseQueryVo.getStart(), personLeaseQueryVo.getEnd()).andBetween("start", personLeaseQueryVo.getStart(), personLeaseQueryVo.getEnd());
        }

        if(personLeaseQueryVo.getPayMainAccountId()!=null){
            criteria.andEqualTo("payMainAccountId",personLeaseQueryVo.getPayMainAccountId());
        }
        if (personLeaseQueryVo.getCampusId()!=null){
            criteria.andEqualTo("campusId",personLeaseQueryVo.getCampusId());
        }

        if (personLeaseQueryVo.getRoomIds()!=null && personLeaseQueryVo.getRoomIds().size()>0){
            criteria.andIn("roomId",personLeaseQueryVo.getRoomIds());
        }

        if (personLeaseQueryVo.getLeaseId() != null) {
            criteria.andEqualTo("leaseId", personLeaseQueryVo.getLeaseId());
        }

        if (personLeaseQueryVo.getRoomId() != null) {
            criteria.andEqualTo("roomId", personLeaseQueryVo.getRoomId());
        }
        if (personLeaseQueryVo.getGreateEndTime() != null) {
            criteria.andGreaterThan("end", personLeaseQueryVo.getGreateEndTime());
        }

        if (personLeaseQueryVo.getProductId() != null) {
            criteria.andEqualTo("productId", personLeaseQueryVo.getProductId());
        }

        if (personLeaseQueryVo.getBusTeamId() != null && personLeaseQueryVo.getBusTeamId() != 0) {
            criteria.andEqualTo("busTeamId", personLeaseQueryVo.getBusTeamId());
        } else {
            if (personLeaseQueryVo.getAreaTeamId() != null) {
                criteria.andEqualTo("areaTeamId", personLeaseQueryVo.getAreaTeamId());
            }
        }
        if (personLeaseQueryVo.getLeaseState() != null) {
            criteria.andEqualTo("leaseState", personLeaseQueryVo.getLeaseState());
        }
        if (personLeaseQueryVo.getBuyerId() != null) {
            criteria.andEqualTo("buyerId", personLeaseQueryVo.getBuyerId());
        }

        if (StringUtils.isNotEmpty(personLeaseQueryVo.getLeaseStates())) {
            String[] leaseStates = personLeaseQueryVo.getLeaseStates().split(",");
            criteria.andIn("leaseState", Arrays.asList(leaseStates));
        }

        if (StringUtils.isNotEmpty(personLeaseQueryVo.getPersonLeaseIds())) {
            String[] personLeaseIds = personLeaseQueryVo.getPersonLeaseIds().split(",");
            criteria.andIn("personLeaseId", Arrays.asList(personLeaseIds));
        }

        if (personLeaseQueryVo.getEndGreateStart() != null) {
            criteria.andLessThanOrEqualTo("start", personLeaseQueryVo.getEndGreateStart());
        }
        if (personLeaseQueryVo.getStartLessEnd() != null) {
            criteria.andGreaterThanOrEqualTo("end", personLeaseQueryVo.getStartLessEnd());
        }
        if (StringUtils.isNotEmpty(personLeaseQueryVo.getEndTimeGreateStartTime())) {
            criteria.andLessThanOrEqualTo("startTime", personLeaseQueryVo.getEndTimeGreateStartTime());
        }
        if (StringUtils.isNotEmpty(personLeaseQueryVo.getStartTimeLessEndTime())) {
            criteria.andGreaterThanOrEqualTo("endTime", personLeaseQueryVo.getStartTimeLessEndTime());
        }


        if (personLeaseQueryVo.getWeekNum() != null) {
            criteria.andEqualTo("weekNum", personLeaseQueryVo.getWeekNum());
        }

        if (personLeaseQueryVo.getOrderByStart() != null) {
            example.orderBy("start");
        }
        if (personLeaseQueryVo.getOrderByStartTime() != null) {
            example.orderBy("startTime");
        }

        if (StringUtils.isNotEmpty(personLeaseQueryVo.getOrderByTimeTypeDesc())) {
            example.orderBy("timeType").desc();
        }


        return findListByExample(example);
    }



    @Override
    public PersonLeaseResultVo findPersonLeaseTime(SysUser sysUser, PersonLeaseSearchVo personLeaseSearchVo) {
        PersonLeaseResultVo personLeaseResultVo=new PersonLeaseResultVo();
        List<PersonLeaseMessage> resultList = new ArrayList<PersonLeaseMessage>();
        if (personLeaseSearchVo.getProductId() == null) {
            throw new BusException("缺乏具体的租赁信息");
        }

        if (personLeaseSearchVo.getStartDate() == null || personLeaseSearchVo.getEndDate() == null) {
            throw new BusException("请输入起止日期");
        }
        if (personLeaseSearchVo.getIsHour() != null && personLeaseSearchVo.getIsHour() == 1 && (StringUtils.isEmpty(personLeaseSearchVo.getStartTime()) || StringUtils.isEmpty(personLeaseSearchVo.getEndTime()))) {
            throw new BusException("选择小时租赁的方式缺乏起止时间");
        } else if (personLeaseSearchVo.getIsHour()==null || personLeaseSearchVo.getIsHour()==0){
            personLeaseSearchVo.setStartTime("00:00");
            personLeaseSearchVo.setEndTime("24:00");
        }
        if (personLeaseSearchVo.getIsEveryWeek() != null && personLeaseSearchVo.getIsEveryWeek() == 1 && personLeaseSearchVo.getWeekNum() == null) {
            throw new BusException("缺乏选择每周几");
        }
        int dayNum = 1;
        if (personLeaseSearchVo.getIsEveryWeek()!=null && personLeaseSearchVo.getIsEveryWeek() == 1) {
            int firstWeek = TimeUtils.getWeekOfDate(personLeaseSearchVo.getStartDate());
            int dataNum = 0;
            if (personLeaseSearchVo.getWeekNum() > firstWeek) {
                dataNum = personLeaseSearchVo.getWeekNum() - firstWeek;
            } else if (personLeaseSearchVo.getWeekNum() < firstWeek) {
                dataNum = 6 - firstWeek + personLeaseSearchVo.getWeekNum() + 1;
            }
            if (dataNum != 0) {
                personLeaseSearchVo.setStartDate(TimeUtils.changeDateByNum(personLeaseSearchVo.getStartDate(), dataNum));
            }
            Integer days = TimeUtils.daysOfTwo(personLeaseSearchVo.getStartDate(), personLeaseSearchVo.getEndDate());
            if (days < 0) {
                throw new BusException("选择的时间段不包含" + weekNameArray[personLeaseSearchVo.getWeekNum()]);
            }
            int extraDays = days % 7;
            personLeaseSearchVo.setEndDate(TimeUtils.changeDateByNum(personLeaseSearchVo.getEndDate(), -extraDays));
            dayNum = 7;
        }
        ServiceProduct serviceProduct = serviceProductService.findById(personLeaseSearchVo.getProductId());
        Campus campus = campusService.findById(serviceProduct.getCampusId());
        String firstYear = Utils.formatDate(personLeaseSearchVo.getStartDate(), "yyyy");
        String lastYear = Utils.formatDate(personLeaseSearchVo.getEndDate(), "yyyy");
        List<String> yearList = new ArrayList<String>();
        Integer firstYearNum = Integer.parseInt(firstYear);
        Integer lastYearNum = Integer.parseInt(lastYear);
        while (firstYearNum <= lastYearNum) {
            yearList.add(firstYearNum.toString());
            firstYearNum++;
        }

        List<SummerWinterDate> summerWinterDateList = summerWinterDateService.findByAreaTeamIdAndYears(campus.getAreaTeamId(), yearList);
        Map<String, SummerWinterDate> summerWinterDateMap = BeanUtils.toMap(summerWinterDateList, "year");

        LeaseMessageQueryVo leaseMessageQueryVo = new LeaseMessageQueryVo();
        leaseMessageQueryVo.setProductId(personLeaseSearchVo.getProductId());
        leaseMessageQueryVo.setStartLessEnd(personLeaseSearchVo.getStartDate());
        leaseMessageQueryVo.setEndGreateStart(personLeaseSearchVo.getEndDate());
        leaseMessageQueryVo.setOrderByStart("yes");
        List<LeaseMessage> leaseMessageList = leaseMessageService.findLeaseMessage(leaseMessageQueryVo);
        List<String> sourcePersonLeaseIdsList = BeanUtils.toList(leaseMessageList, "sourcePersonLeaseIds");
        Set<Long> personLeaseIdSet = new HashSet<Long>();
        for (String sourcePersonLeaseIds : sourcePersonLeaseIdsList) {
            if (StringUtils.isNotEmpty(sourcePersonLeaseIds)) {
                personLeaseIdSet.addAll(Utils.toLongIds(sourcePersonLeaseIds));
            }
        }
        BitSet tryLeaseSet = new BitSet();
        List<TimeStampLong> tryTimeStampLong = this.findTimeStampLongBySearch(personLeaseSearchVo);
        for (TimeStampLong timeStampLong : tryTimeStampLong) {
            tryLeaseSet.set(timeStampLong.getStartTimeStamp(), timeStampLong.getEndTimeStamp());
        }
        BitSet otherTryLeaseSet = (BitSet) tryLeaseSet.clone();
        BitSet conflictCheckSet = (BitSet) tryLeaseSet.clone();
        if (personLeaseIdSet.size() > 0) {
            PersonLeaseQueryVo personLeaseQueryVo = new PersonLeaseQueryVo();
            personLeaseQueryVo.setPersonLeaseIds(StringUtils.join(personLeaseIdSet, ","));
            personLeaseQueryVo.setEndGreateStart(personLeaseSearchVo.getStartDate());
            personLeaseQueryVo.setStartLessEnd(personLeaseSearchVo.getEndDate());
            personLeaseQueryVo.setStartTimeLessEndTime(personLeaseSearchVo.getEndTime());
            personLeaseQueryVo.setEndTimeGreateStartTime(personLeaseSearchVo.getStartTime());
            List<PersonLeaseMessage> personLeaseMessageList = this.findPersonLeaseMessage(personLeaseQueryVo);
            if (personLeaseMessageList == null || personLeaseMessageList.size() <= 0) {
                throw new BusException("所选的时间段不存在可以租的时间");
            }
            BitSet haveLeaseSet = new BitSet();
            for (PersonLeaseMessage personLeaseMessage : personLeaseMessageList) {
                List<TimeStampLong> timeStampLongs = this.findTimeStampLongByPersonLease(personLeaseMessage);
                for (TimeStampLong timeStampLong : timeStampLongs) {
                    haveLeaseSet.set(timeStampLong.getStartTimeStamp(), timeStampLong.getEndTimeStamp());
                }
            }
            tryLeaseSet.andNot(haveLeaseSet);
            if (tryLeaseSet.cardinality() != 0) {
                throw new BusException("所选时间段存在某个时间段无时间出租");
            }
        }

        BitSet leaseBitSet = new BitSet();
        for (LeaseMessage leaseMessage : leaseMessageList) {
            leaseBitSet.set(TimeUtils.toMini(leaseMessage.getStart()), TimeUtils.toMini(TimeUtils.changeDateByNum(leaseMessage.getEnd(),1)));
        }
        otherTryLeaseSet.andNot(leaseBitSet);
        if (otherTryLeaseSet.cardinality() != 0) {
            throw new BusException("所选时间段存在某个时间段无时间出租");
        }
        PersonLeaseQueryVo personLeaseQueryVo = new PersonLeaseQueryVo();
        personLeaseQueryVo.setProductId(serviceProduct.getProductId());
        //personLeaseQueryVo.setRoomId(serviceProduct.getRoomId());
        personLeaseQueryVo.setEndTimeGreateStartTime(personLeaseSearchVo.getEndTime());
        personLeaseQueryVo.setStartTimeLessEndTime(personLeaseSearchVo.getStartTime());
        personLeaseQueryVo.setStartLessEnd(personLeaseSearchVo.getStartDate());
        personLeaseQueryVo.setEndGreateStart(personLeaseSearchVo.getEndDate());
        //personLeaseQueryVo.setWeekNum(personLeaseSearchVo.getWeekNum());
        personLeaseQueryVo.setLeaseStates("0,1");
        personLeaseQueryVo.setOrderByStart("yes");
        List<PersonLeaseMessage> personLeaseMessageList = this.findPersonLeaseMessage(personLeaseQueryVo);
         /*查找直接被排课了的时间*/
        PersonLeaseQueryVo newPersonLeaseQueryVo = new PersonLeaseQueryVo();
        newPersonLeaseQueryVo.setRoomId(serviceProduct.getRoomId());
        newPersonLeaseQueryVo.setEndTimeGreateStartTime(personLeaseSearchVo.getEndTime());
        newPersonLeaseQueryVo.setStartTimeLessEndTime(personLeaseSearchVo.getStartTime());
        newPersonLeaseQueryVo.setStartLessEnd(personLeaseSearchVo.getStartDate());
        newPersonLeaseQueryVo.setEndGreateStart(personLeaseSearchVo.getEndDate());
        newPersonLeaseQueryVo.setLeaseState(999);
        newPersonLeaseQueryVo.setOrderByStart("yes");
        List<PersonLeaseMessage> hasUsePersonLeaseMessageList = this.findPersonLeaseMessage(newPersonLeaseQueryVo);
        if (hasUsePersonLeaseMessageList!=null && hasUsePersonLeaseMessageList.size()>0){
            personLeaseMessageList.addAll(hasUsePersonLeaseMessageList);
        }


        if (personLeaseMessageList.size() > 0) {
            BitSet conflictSet=new BitSet();
            for (PersonLeaseMessage personLeaseMessage:personLeaseMessageList){
                List<TimeStampLong> timeStampLongList=this.findTimeStampLongByPersonLease(personLeaseMessage);
                for (TimeStampLong timeStampLong:timeStampLongList){
                    conflictSet.set(timeStampLong.getStartTimeStamp(),timeStampLong.getEndTimeStamp());
                }
                Date newStart=personLeaseMessage.getStart().getTime()>personLeaseSearchVo.getStartDate().getTime()?personLeaseMessage.getStart():personLeaseSearchVo.getStartDate();
                Date newEnd=personLeaseMessage.getEnd().getTime()<personLeaseSearchVo.getEndDate().getTime()?personLeaseMessage.getEnd():personLeaseSearchVo.getEndDate();
                String newTimeDesc=Utils.formatDate(newStart,"yyyy-MM-dd")+"到"+Utils.formatDate(newEnd,"yyyy-MM-dd");
                newTimeDesc=newTimeDesc+" "+((personLeaseMessage.getWeekNum()==null || personLeaseMessage.getWeekNum()==-1)? "":(weekNameArray[personLeaseMessage.getWeekNum()]));
                Integer tempNum=1;
                if (personLeaseMessage.getWeekNum()!=null && personLeaseMessage.getWeekNum()!=-1){
                    tempNum=7;
                }
                Integer newHourNum=1;
                Integer allDayNum=TimeUtils.daysOfTwo(newStart,newEnd)+1;
                Integer newDayNum=allDayNum/tempNum+allDayNum%tempNum;
                BigDecimal hourNum=BigDecimal.ONE;
                if (personLeaseMessage.getTimeType()!=null && personLeaseMessage.getTimeType()==-1){
                    String newStartTime=personLeaseMessage.getStartTime().compareTo(personLeaseSearchVo.getStartTime())>0?personLeaseMessage.getStartTime():personLeaseSearchVo.getStartTime();
                    String newEndTime=personLeaseMessage.getEndTime().compareTo(personLeaseSearchVo.getEndTime())<0?personLeaseMessage.getEndTime():personLeaseSearchVo.getEndTime();
                    newTimeDesc=newTimeDesc+" "+newStartTime+"到"+newEndTime;
                    newHourNum=Integer.parseInt(newEndTime.split(":")[0]) - Integer.parseInt(newStartTime.split(":")[0]);
                    hourNum=TimeUtils.countHourNum(newStartTime,newEndTime).multiply(new BigDecimal(newDayNum));
                }
                personLeaseMessage.setDayTimeNum(hourNum.multiply(new BigDecimal(allDayNum)));
                personLeaseMessage.setTimeNum(newDayNum*newHourNum);
                personLeaseMessage.setTimeDesc(newTimeDesc);
            }
            conflictCheckSet.andNot(conflictSet);
            if (conflictCheckSet.cardinality() == 0) {
                throw new BusException("所选时间段已经全部被租完");
            }
            Iterator<PersonLeaseMessage> iterator=personLeaseMessageList.iterator();
            while (iterator.hasNext()){
                PersonLeaseMessage personLeaseMessage=iterator.next();
                if (personLeaseSearchVo.getWeekNum()!=null && personLeaseMessage.getWeekNum()!=null && personLeaseSearchVo.getWeekNum()!=-1 && personLeaseMessage.getWeekNum()!=-1 && !personLeaseMessage.getWeekNum().equals(personLeaseSearchVo.getWeekNum())){
                    iterator.remove();
                }
            }


            personLeaseResultVo.setConflictList(personLeaseMessageList);
        }
        String confictIds=StringUtils.join(BeanUtils.toList(personLeaseMessageList,"personLeaseId"),",");
        TimePriceVo timePriceVo=new TimePriceVo();
        timePriceVo.setSummerWinterDateList(summerWinterDateList);
        List<LeasePriceVo> leasePriceVoList=new ArrayList<LeasePriceVo>(leaseMessageList.size());
        for (LeaseMessage leaseMessage:leaseMessageList){
            LeasePriceVo leasePriceVo=new LeasePriceVo();
            BeanUtils.copyProperties(leasePriceVo,leaseMessage);
            leasePriceVoList.add(leasePriceVo);
        }
        timePriceVo.setLeasePriceVoList(leasePriceVoList);
        String timePrice= JSON.toJSONString(timePriceVo);
        String timeDesc=Utils.formatDate(personLeaseSearchVo.getStartDate(),"yyyy-MM-dd")+"到"+Utils.formatDate(personLeaseSearchVo.getEndDate(),"yyyy-MM-dd")+" "+
                (((personLeaseSearchVo.getWeekNum()==null || personLeaseSearchVo.getWeekNum()==-1)? " ":(weekNameArray[personLeaseSearchVo.getWeekNum()]))+" "+ ((personLeaseSearchVo.getIsHour()==null || personLeaseSearchVo.getIsHour()==0)?"":(personLeaseSearchVo.getStartTime()+"到"+personLeaseSearchVo.getEndTime())));

        PersonLeaseMessage personLeaseMessage = new PersonLeaseMessage();
        personLeaseMessage.setWeekNum(personLeaseSearchVo.getWeekNum());
        personLeaseMessage.setTimePrice(timePrice);
        personLeaseMessage.setStartTime(personLeaseSearchVo.getStartTime());
        personLeaseMessage.setEndTime(personLeaseSearchVo.getEndTime());
        personLeaseMessage.setStart(personLeaseSearchVo.getStartDate());
        personLeaseMessage.setEnd(personLeaseSearchVo.getEndDate());
        Integer timeNum=0;
        if (personLeaseSearchVo.getIsHour()!=null && personLeaseSearchVo.getIsHour() == 1) {
            personLeaseMessage.setTimeType(-1);
            Integer hour = Integer.parseInt(personLeaseSearchVo.getEndTime().split(":")[0]) - Integer.parseInt(personLeaseSearchVo.getStartTime().split(":")[0]);
            BigDecimal totalPrice = BigDecimal.ZERO;
            Date startDate = personLeaseSearchVo.getStartDate();
            while (startDate.getTime() <= personLeaseSearchVo.getEndDate().getTime()) {
                for (LeaseMessage leaseMessage : leaseMessageList) {
                    String year = Utils.formatDate(startDate, "yyyy");
                    SummerWinterDate summerWinterDate = summerWinterDateMap.get(year);
                    if (leaseMessage.getsWHourPrice() != null && summerWinterDate != null && ((startDate.getTime() >= summerWinterDate.getWinterStartDate().getTime() && startDate.getTime() <= summerWinterDate.getWinterEndDate().getTime())
                            || (startDate.getTime() >= summerWinterDate.getSummerStartDate().getTime() && startDate.getTime()<=summerWinterDate.getSummerEndDate().getTime()))
                            ){
                        totalPrice = totalPrice.add(leaseMessage.getsWHourPrice());
                    } else if (leaseMessage.getWeekendHourPrice()!=null && leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() && (TimeUtils.getWeekOfDate(startDate) == 6 || TimeUtils.getWeekOfDate(startDate) == 0)) {
                        totalPrice = totalPrice.add(leaseMessage.getWeekendHourPrice());
                    } else if (leaseMessage.getWorkingHourPrice()!=null && leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() ){
                        totalPrice = totalPrice.add(leaseMessage.getWorkingHourPrice());
                    }
                }
                startDate = TimeUtils.changeDateByNum(startDate, dayNum);
                timeNum=timeNum+hour;
            }
            personLeaseMessage.setTotalPrice(totalPrice.multiply(new BigDecimal(hour)));
            personLeaseMessage.setTimeDesc(timeDesc);
            personLeaseMessage.setTimeNum(timeNum);
            personLeaseMessage.setConfictIds(confictIds);
            resultList.add(personLeaseMessage);
        }else if (personLeaseSearchVo.getIsHour() == 0 && personLeaseSearchVo.getIsEveryWeek()==1) {
            personLeaseMessage.setTimeType(0);
            BigDecimal totalPrice = BigDecimal.ZERO;
            Date startDate = personLeaseSearchVo.getStartDate();
            while (startDate.getTime() <= personLeaseSearchVo.getEndDate().getTime()) {
                for (LeaseMessage leaseMessage : leaseMessageList) {
                    String year = Utils.formatDate(startDate, "yyyy");
                    SummerWinterDate summerWinterDate = summerWinterDateMap.get(year);
                    if (leaseMessage.getsWDayPrice() != null && summerWinterDate != null && ((startDate.getTime() >= summerWinterDate.getWinterStartDate().getTime() && startDate.getTime() <= summerWinterDate.getWinterEndDate().getTime())
                            || (startDate.getTime() >= summerWinterDate.getSummerStartDate().getTime() && startDate.getTime()<=summerWinterDate.getSummerEndDate().getTime()))
                            ){
                        totalPrice = totalPrice.add(leaseMessage.getsWDayPrice());
                    } else if (leaseMessage.getWeekendDayPrice()!=null &&leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() && (TimeUtils.getWeekOfDate(startDate) == 6 || TimeUtils.getWeekOfDate(startDate) == 0)) {
                        totalPrice = totalPrice.add(leaseMessage.getWeekendDayPrice());
                    } else if (leaseMessage.getWorkingDayPrice()!=null && leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() ){
                        totalPrice = totalPrice.add(leaseMessage.getWorkingDayPrice());
                    }
                }
                startDate = TimeUtils.changeDateByNum(startDate, dayNum);
                timeNum=timeNum+1;
            }
            personLeaseMessage.setTotalPrice(totalPrice);
            personLeaseMessage.setTimeDesc(timeDesc);
            personLeaseMessage.setTimeNum(timeNum);
            personLeaseMessage.setConfictIds(confictIds);
            resultList.add(personLeaseMessage);
        }else {
            personLeaseMessage.setTimeType(0);
            timeNum=TimeUtils.daysOfTwo(personLeaseSearchVo.getStartDate(),personLeaseSearchVo.getEndDate())+1;
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (LeaseMessage leaseMessage:leaseMessageList){
                Date startDate=personLeaseSearchVo.getStartDate().getTime()<leaseMessage.getStart().getTime()?leaseMessage.getStart():personLeaseSearchVo.getStartDate();
                Date endDate=personLeaseSearchVo.getEndDate().getTime()>leaseMessage.getEnd().getTime()?leaseMessage.getEnd():personLeaseSearchVo.getEndDate();
                Integer yearNum=0;
                while (TimeUtils.changeDateByNum(TimeUtils.addYearByNum(startDate,1),-1).getTime()<=endDate.getTime()){
                    yearNum++;
                    startDate=TimeUtils.addYearByNum(startDate,1);
                }
                if (yearNum>0){
                    totalPrice=totalPrice.add(leaseMessage.getYearPrice().multiply(new BigDecimal(yearNum)));
                }

                Integer monthNum=0;
                while (TimeUtils.changeDateByNum(TimeUtils.addMonthByNum(startDate,1),-1).getTime()<=endDate.getTime()){
                    monthNum++;
                    startDate=TimeUtils.addMonthByNum(startDate,1);
                }
                if (monthNum>0){
                    totalPrice=totalPrice.add(leaseMessage.getMonthPrice().multiply(new BigDecimal(monthNum)));
                }
                if (startDate.getTime()<=endDate.getTime()){
                    while (startDate.getTime()<=endDate.getTime()){
                        String year = Utils.formatDate(startDate, "yyyy");
                        SummerWinterDate summerWinterDate = summerWinterDateMap.get(year);
                        if (leaseMessage.getsWDayPrice() != null && summerWinterDate != null && ((startDate.getTime() >= summerWinterDate.getWinterStartDate().getTime() && startDate.getTime() <= summerWinterDate.getWinterEndDate().getTime())
                                || (startDate.getTime() >= summerWinterDate.getSummerStartDate().getTime() && startDate.getTime()<=summerWinterDate.getSummerEndDate().getTime()))
                                ) {
                            totalPrice = totalPrice.add(leaseMessage.getsWDayPrice());
                        } else if (leaseMessage.getWeekendDayPrice()!=null && leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() && (TimeUtils.getWeekOfDate(startDate) == 6 || TimeUtils.getWeekOfDate(startDate) == 0)) {
                            totalPrice = totalPrice.add(leaseMessage.getWeekendDayPrice());
                        } else if (leaseMessage.getWorkingDayPrice()!=null && leaseMessage.getStart().getTime()<=startDate.getTime() && leaseMessage.getEnd().getTime()>=startDate.getTime() ){
                            totalPrice = totalPrice.add(leaseMessage.getWorkingDayPrice());
                        }
                        startDate=TimeUtils.changeDateByNum(startDate,dayNum);
                    }
                }
            }
            personLeaseMessage.setTotalPrice(totalPrice);

            personLeaseMessage.setTimeDesc(timeDesc);
            personLeaseMessage.setTimeNum(timeNum);
            personLeaseMessage.setConfictIds(confictIds);
            resultList.add(personLeaseMessage);
        }


        personLeaseResultVo.setPersonLeaseMessageList(resultList);

        return personLeaseResultVo;
    }

    @Override
    public List<TimeStampLong> findTimeStampLongByPersonLease(PersonLeaseMessage personLeaseMessage) {
        List<TimeStampLong> timeStampLongList = new ArrayList<TimeStampLong>();
        Date startDate = personLeaseMessage.getStart();
        Date endDate = personLeaseMessage.getEnd();
        String startTime = personLeaseMessage.getStartTime();
        String endTime = personLeaseMessage.getEndTime();
        Integer weekNum = personLeaseMessage.getWeekNum();

        while (startDate.getTime() <= endDate.getTime()) {
            TimeStampLong timeStampLong = new TimeStampLong();
            String startStr = Utils.formatDate(startDate, "yyyy-MM-dd");
            String tempStartStr = startStr + " " + startTime;
            Date tempStartDate = Utils.parseDate(tempStartStr, "yyyy-MM-dd HH:mm");
            String tempEndStr = startStr + " " + endTime;
            Date tempEndDate = Utils.parseDate(tempEndStr, "yyyy-MM-dd HH:mm");
            timeStampLong.setStartTimeStamp(TimeUtils.toMini(tempStartDate));
            timeStampLong.setEndTimeStamp(TimeUtils.toMini(tempEndDate));
            timeStampLongList.add(timeStampLong);
            if (weekNum != null && weekNum != -1) {
                startDate = TimeUtils.changeDateByNum(startDate, 7);
            } else {
                startDate = TimeUtils.changeDateByNum(startDate, 1);
            }
        }
        return timeStampLongList;
    }

    @Override
    public List<TimeStampLong> findTimeStampLongBySearch(PersonLeaseSearchVo personLeaseSearchVo) {
        List<TimeStampLong> timeStampLongList = new ArrayList<TimeStampLong>();
        Date startDate = personLeaseSearchVo.getStartDate();
        Date endDate = personLeaseSearchVo.getEndDate();
        String startTime = personLeaseSearchVo.getStartTime();
        String endTime = personLeaseSearchVo.getEndTime();
        Integer weekNum = personLeaseSearchVo.getWeekNum();

        while (startDate.getTime() <= endDate.getTime()) {
            TimeStampLong timeStampLong = new TimeStampLong();
            String startStr = Utils.formatDate(startDate, "yyyy-MM-dd");
            String tempStartStr = startStr + " " + startTime;
            Date tempStartDate = Utils.parseDate(tempStartStr, "yyyy-MM-dd HH:mm");
            String tempEndStr = startStr + " " + endTime;
            Date tempEndDate = Utils.parseDate(tempEndStr, "yyyy-MM-dd HH:mm");
            timeStampLong.setStartTimeStamp(TimeUtils.toMini(tempStartDate));
            timeStampLong.setEndTimeStamp(TimeUtils.toMini(tempEndDate));
            timeStampLongList.add(timeStampLong);
            if (weekNum != null && weekNum != -1) {
                startDate = TimeUtils.changeDateByNum(startDate, 7);
            } else {
                startDate = TimeUtils.changeDateByNum(startDate, 1);
            }
        }
        return timeStampLongList;
    }

    @Override
    @Transactional
    public List<PersonLeaseMessage> addOrBuyPersonLeaseMessage(Integer check, Integer state, SysUser sysUser, List<PersonLeaseMessage> personLeaseMessageList, ServiceProduct serviceProduct,Long payMainAccountId) {

        List<LeaseTimeDetail> leaseTimeDetailList = new ArrayList<>();
        for (PersonLeaseMessage personLeaseMessage : personLeaseMessageList) {
            if (state == 1) {
                personLeaseMessage.setCreateTime(TimeUtils.now());
                personLeaseMessage.setUpdateTime(TimeUtils.now());
                personLeaseMessage.setRoomName(serviceProduct.getRoomName());
                personLeaseMessage.setRoomId(serviceProduct.getRoomId());
                personLeaseMessage.setCampusId(serviceProduct.getCampusId());
                personLeaseMessage.setCampusName(serviceProduct.getCampusName());
                personLeaseMessage.setBuyerId(sysUser.getUserId());
                personLeaseMessage.setBuyerName(sysUser.getUsername());
                personLeaseMessage.setLeaseState(9);
                personLeaseMessage.setProductId(serviceProduct.getProductId());
            }

            List<TimeStampLong> timeStampLongList = findTimeStampLongByPersonLease(personLeaseMessage);
            LeaseTimeDetail leaseTimeDetail = new LeaseTimeDetail();
            leaseTimeDetail.setTimeStampLongList(timeStampLongList);
            leaseTimeDetail.setDesc(personLeaseMessage.getTimeDesc());
            leaseTimeDetailList.add(leaseTimeDetail);
        }

        if (leaseTimeDetailList.size() > 0) {

            List<PersonLeaseMessage> personLeaseMessages=new ArrayList<PersonLeaseMessage>();

            /*自身清单信息加入清单时过滤*/
            if (check == 1) {
                PersonLeaseQueryVo personLeaseQueryVo1 = new PersonLeaseQueryVo();
                personLeaseQueryVo1.setProductId(serviceProduct.getProductId());
                personLeaseQueryVo1.setGreateEndTime(TimeUtils.now());
                personLeaseQueryVo1.setLeaseStates("9");//租赁清单未产生订单
                personLeaseQueryVo1.setBuyerId(sysUser.getUserId());
                // personLeaseQueryVo1.setLeaseId(leaseId);
                List<PersonLeaseMessage> personLeaseMessages1 = this.findPersonLeaseMessage(personLeaseQueryVo1);

                personLeaseMessages.addAll(personLeaseMessages1);
            }

            List<TimeStampLong> allHaveTimeStampLongList = new ArrayList<TimeStampLong>();

            List<LeaseTimeDetail> haveLeaseTimeDetailList = new ArrayList<>(personLeaseMessages == null ? 0 : personLeaseMessages.size());
            for (PersonLeaseMessage personLeaseMessage : personLeaseMessages) {
                List<TimeStampLong> timeStampLongList = findTimeStampLongByPersonLease(personLeaseMessage);
                allHaveTimeStampLongList.addAll(timeStampLongList);
                LeaseTimeDetail leaseTimeDetail = new LeaseTimeDetail();
                leaseTimeDetail.setTimeStampLongList(timeStampLongList);
                leaseTimeDetail.setDesc(personLeaseMessage.getTimeDesc());
                haveLeaseTimeDetailList.add(leaseTimeDetail);
            }
            for (LeaseTimeDetail leaseTimeDetail : leaseTimeDetailList) {
                List<TimeStampLong> timeStampLongList = leaseTimeDetail.getTimeStampLongList();
                for (TimeStampLong timeStampLong : timeStampLongList) {
                    Integer start = timeStampLong.getStartTimeStamp();
                    Integer end = timeStampLong.getEndTimeStamp();
                    for (LeaseTimeDetail haveLeaseTimeDetail : haveLeaseTimeDetailList) {
                        for (TimeStampLong havTimeStampLong : haveLeaseTimeDetail.getTimeStampLongList()) {
                            Integer checkStart = havTimeStampLong.getStartTimeStamp();
                            Integer checkEnd = havTimeStampLong.getEndTimeStamp();
                            if (start >= checkEnd || end <= checkStart) {
                                continue;
                            } else {
                                throw new BusException(leaseTimeDetail.getDesc() + "与加入购物车的时间段" + haveLeaseTimeDetail.getDesc() + "冲突");
                            }
                        }
                    }
                }
            }
            if (state == 1) {
                this.insertBatch(personLeaseMessageList);
            } else if (state == 2) {
                List<Long> personLeaseIds = BeanUtils.toList(personLeaseMessageList, "personLeaseId");
                if (personLeaseIds.size() > 0) {
                    this.updatePayIdAndLeaseState(personLeaseIds, 0,payMainAccountId);//更改为未付款
                }
            }


        }
        return personLeaseMessageList;
    }

    @Override
    public void changeLeaseStateByIds(List<Long> personLeaseIds, Integer leaseState) {
        PersonLeaseMessage personLeaseMessage = new PersonLeaseMessage();
        personLeaseMessage.setLeaseState(leaseState);
        personLeaseMessage.setUpdateTime(TimeUtils.now());
        Example<PersonLeaseMessage> example = PersonLeaseMessage.createExample();
        example.createCriteria().andIn("personLeaseId", personLeaseIds);
        this.updateSelectiveByExample(personLeaseMessage, example);
    }

    @Override
    public void updatePayIdAndLeaseState(List<Long> personLeaseIds, Integer leaseState, Long payMainAccountId) {
        PersonLeaseMessage personLeaseMessage = new PersonLeaseMessage();
        personLeaseMessage.setLeaseState(leaseState);
        personLeaseMessage.setUpdateTime(TimeUtils.now());
        personLeaseMessage.setPayMainAccountId(payMainAccountId);
        Example<PersonLeaseMessage> example = PersonLeaseMessage.createExample();
        example.createCriteria().andIn("personLeaseId", personLeaseIds);
        this.updateSelectiveByExample(personLeaseMessage, example);
    }

    @Override
    public List<PersonLeaseMessage> findByIdsAndDate(List<Long> personLeaseIds, Date date) {
        Example<PersonLeaseMessage> example = PersonLeaseMessage.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("personLeaseId", personLeaseIds).andLessThanOrEqualTo("start", date).andGreaterThanOrEqualTo("end", date);
        return this.findListByExample(example);
    }

    @Override
    public List<PersonLeaseMessage> changeTimeDesc(PersonLeaseMessage personLeaseMessage, List<PersonLeaseMessage> personLeaseMessageList) {
        for (PersonLeaseMessage tempMessage:personLeaseMessageList){

            /*相同主体的预定冲突时间去除*/
            if (tempMessage.getPayMainAccountId().equals(personLeaseMessage.getPayMainAccountId()) && tempMessage.getLeaseState().equals(999)){
                continue;
            }
            Date newStart=tempMessage.getStart().getTime()>personLeaseMessage.getStart().getTime()?tempMessage.getStart():personLeaseMessage.getStart();
            Date newEnd=tempMessage.getEnd().getTime()<personLeaseMessage.getEnd().getTime()?tempMessage.getEnd():personLeaseMessage.getEnd();
            String newTimeDesc=Utils.formatDate(newStart,"yyyy-MM-dd")+"到"+Utils.formatDate(newEnd,"yyyy-MM-dd");
            newTimeDesc=newTimeDesc+((tempMessage.getWeekNum()==null || tempMessage.getWeekNum()==-1)? "":(weekNameArray[tempMessage.getWeekNum()]));
            Integer tempNum=1;
            if (tempMessage.getWeekNum()!=null && tempMessage.getWeekNum()!=-1){
                tempNum=7;
            }
            Integer newHourNum=1;
            Integer allDayNum=TimeUtils.daysOfTwo(newStart,newEnd)+1;
            Integer newDayNum=allDayNum/tempNum+allDayNum%tempNum;
            if (tempMessage.getTimeType()!=null && tempMessage.getTimeType()==-1){
                String newStartTime=tempMessage.getStartTime().compareTo(personLeaseMessage.getStartTime())>0?tempMessage.getStartTime():personLeaseMessage.getStartTime();
                String newEndTime=tempMessage.getEndTime().compareTo(personLeaseMessage.getEndTime())<0?tempMessage.getEndTime():personLeaseMessage.getEndTime();
                newTimeDesc=newTimeDesc+" "+newStartTime+"到"+newEndTime;
                newHourNum=Integer.parseInt(newEndTime.split(":")[0]) - Integer.parseInt(newStartTime.split(":")[0]);
            }
            tempMessage.setTimeNum(newDayNum*newHourNum);
            tempMessage.setTimeDesc(newTimeDesc);
        }
        return personLeaseMessageList;
    }

    public Page<Room> findRoomByBuyOrCreate(Long classId, SysUser sysUser,Long campusId,String roomName,int pageNo,int pageSize) {
        List<Long> realRoomIdList=new ArrayList<Long>();
        CourseClass courseClass=courseClassService.findById(classId);
        Long busTeamId=courseClass.getBusTeamId();
        Long areaTeamId=courseClass.getAreaTeamId();
        List<ClassLecture> classLectureList=classLectureService.findClassLectureByClassId(classId);
        BitSet bitSet=new BitSet();
        Date minDate=null;
        Date maxDate=null;
        for (ClassLecture classLecture:classLectureList){
            String startStr= Utils.formatDate(classLecture.getClassDate(),"yyyy-MM-dd")+" "+classLecture.getStartTime();
            Date startDate=Utils.parseDate(startStr,"yyyy-MM-dd HH:mm");
            String endStr=Utils.formatDate(classLecture.getClassDate(),"yyyy-MM-dd")+" "+classLecture.getEndTime();
            Date endDate=Utils.parseDate(endStr,"yyyy-MM-dd HH:mm");
            bitSet.set(TimeUtils.toMini(startDate),TimeUtils.toMini(endDate));
            if (minDate==null || minDate.getTime()>startDate.getTime()){
                minDate=startDate;
            }
            if (maxDate==null || maxDate.getTime()<endDate.getTime()){
                maxDate=endDate;
            }
        }
        if (sysUser.getUserType()==1 || sysUser.getUserType()==0){
            List<Campus> campusList=campusService.findCampusByAreaTeamId(areaTeamId);
            List<Long> campusIdList=BeanUtils.toList(campusList,"campusId");
            List<Room> roomList=roomService.findByCond(campusIdList,campusId,roomName,1);
            List<Long> roomIdList=BeanUtils.toList(roomList,"roomId");
            if (roomIdList==null || roomIdList.size()<=0){
                return null;
            }
            PersonLeaseQueryVo personLeaseQueryVo=new PersonLeaseQueryVo();
            personLeaseQueryVo.setRoomIds(roomIdList);
            personLeaseQueryVo.setLeaseStates("0,1");
            personLeaseQueryVo.setEndGreateStart(maxDate);
            personLeaseQueryVo.setStartLessEnd(minDate);
            List<PersonLeaseMessage> personLeaseMessageList=this.findPersonLeaseMessage(personLeaseQueryVo);
            Map<Long,List<PersonLeaseMessage>> map=BeanUtils.toGroup(personLeaseMessageList,"roomId");
            //Iterator<Map.Entry<Long, List<PersonLeaseMessage>>> entries = map.entrySet().iterator();
            for (Room room:roomList){

                Long roomId=room.getRoomId();

                List<PersonLeaseMessage> roomLeaseList=map.get(roomId);
                if (roomLeaseList==null) {
                    realRoomIdList.add(roomId);
                    continue;
                }
                List<TimeStampLong> allTimeStampLongList=new ArrayList<TimeStampLong>();
                List<String> confictIds=new ArrayList<String>();
                for (PersonLeaseMessage personLeaseMessage:roomLeaseList){
                    if (personLeaseMessage.getPayMainAccountId().equals(busTeamId)){
                        realRoomIdList.add(roomId);
                        continue;
                    }
                    allTimeStampLongList.addAll(this.findTimeStampLongByPersonLease(personLeaseMessage));
                    if (StringUtils.isNotEmpty(personLeaseMessage.getConfictIds())){
                        confictIds.add(personLeaseMessage.getConfictIds());
                    }
                }
                BitSet roomBitSet=new BitSet();
                for (TimeStampLong timeStampLong:allTimeStampLongList){
                    roomBitSet.set(timeStampLong.getStartTimeStamp(),timeStampLong.getEndTimeStamp());
                }
                if (confictIds.size()>0){
                    List<TimeStampLong> confictTimeStampLongList=new ArrayList<TimeStampLong>();
                    String str=StringUtils.join(confictIds,",");
                    List<Long> confictIdList=Utils.toLongIds(str);
                    List<PersonLeaseMessage> confictPersonLeaseMessages=this.findByIds(confictIdList);
                    for (PersonLeaseMessage personLeaseMessage:confictPersonLeaseMessages){
                        confictTimeStampLongList.addAll(this.findTimeStampLongByPersonLease(personLeaseMessage));
                    }
                    for (TimeStampLong timeStampLong:confictTimeStampLongList){
                        roomBitSet.clear(timeStampLong.getStartTimeStamp(),timeStampLong.getEndTimeStamp());
                    }
                }
                BitSet cloneBitSet=(BitSet) bitSet.clone();
                if (!cloneBitSet.intersects(roomBitSet)){
                    realRoomIdList.add(roomId);
                }
            }
        }else if (sysUser.getUserType()==2){//业务组
            PersonLeaseQueryVo personLeaseQueryVo=new PersonLeaseQueryVo();
            personLeaseQueryVo.setPayMainAccountId(busTeamId);
            personLeaseQueryVo.setLeaseState(1);
            if (campusId!=null){
                personLeaseQueryVo.setCampusId(campusId);
            }
            personLeaseQueryVo.setEndGreateStart(maxDate);
            personLeaseQueryVo.setStartLessEnd(minDate);
            List<PersonLeaseMessage> personLeaseMessageList=this.findPersonLeaseMessage(personLeaseQueryVo);
            Map<Long,List<PersonLeaseMessage>> map=BeanUtils.toGroup(personLeaseMessageList,"roomId");
            Iterator<Map.Entry<Long, List<PersonLeaseMessage>>> entries = map.entrySet().iterator();
            while (entries.hasNext()){
                Map.Entry<Long, List<PersonLeaseMessage>> entry = entries.next();
                Long roomId=entry.getKey();
                List<PersonLeaseMessage> roomLeaseList=entry.getValue();
                BitSet roomBitSet=new BitSet();
                List<TimeStampLong> timeStampLongList=new ArrayList<TimeStampLong>();
                List<String> confictIds=new ArrayList<String>();
                for (PersonLeaseMessage personLeaseMessage:roomLeaseList){
                    timeStampLongList.addAll(this.findTimeStampLongByPersonLease(personLeaseMessage));
                    if (StringUtils.isNotEmpty(personLeaseMessage.getConfictIds())){
                        confictIds.add(personLeaseMessage.getConfictIds());
                    }
                }
                for (TimeStampLong timeStampLong:timeStampLongList){
                    roomBitSet.set(timeStampLong.getStartTimeStamp(),timeStampLong.getEndTimeStamp());
                }
                if (confictIds.size()>0){
                    List<TimeStampLong> confictTimeStampLongList=new ArrayList<TimeStampLong>();
                    String str=StringUtils.join(confictIds,",");
                    List<Long> confictIdList=Utils.toLongIds(str);
                    List<PersonLeaseMessage> confictPersonLeaseMessages=this.findByIds(confictIdList);
                    for (PersonLeaseMessage personLeaseMessage:confictPersonLeaseMessages){
                        confictTimeStampLongList.addAll(this.findTimeStampLongByPersonLease(personLeaseMessage));
                    }
                    for (TimeStampLong timeStampLong:confictTimeStampLongList){
                        roomBitSet.clear(timeStampLong.getStartTimeStamp(),timeStampLong.getEndTimeStamp());
                    }
                }
                BitSet cloneBitSet=(BitSet) bitSet.clone();
                cloneBitSet.andNot(roomBitSet);
                if (cloneBitSet.cardinality()==0){
                    realRoomIdList.add(roomId);
                }
            }
        }
        Page<Room> roomPage=roomService.findByRoomIds(realRoomIdList,pageNo,pageSize);
        return roomPage;
    }

    @Override
    public void deleteByClassIdAndLeaseState(Long classId, Integer leaseState) {
        if(classId == null || leaseState == null ){
            throw new NullPointerException();
        }
        Example<PersonLeaseMessage> example=PersonLeaseMessage.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("classId",classId);
        criteria.andEqualTo("leaseState",leaseState);
        this.deleteByExample(example);
    }

    @Override
    public void deleteByClassIdsAndLeaseState(List<Long> classIdList, Integer leaseState) {
        if(classIdList == null || classIdList.isEmpty() || leaseState == null ){
            throw new NullPointerException();
        }
        Example<PersonLeaseMessage> example=PersonLeaseMessage.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andIn("classId",classIdList);
        criteria.andEqualTo("leaseState",leaseState);
        this.deleteByExample(example);
    }
}